package com.ruoyi.product.service.impl;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.product.domain.ProdAtta;
import com.ruoyi.product.domain.TbProdInfo;
import com.ruoyi.product.es.EsProd;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.TbProdMapper;
import com.ruoyi.product.domain.TbProd;
import com.ruoyi.product.service.ITbProdService;

/**
 * 商品信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-01
 */
@Service
public class TbProdServiceImpl implements ITbProdService 
{
    @Autowired
    private TbProdMapper tbProdMapper;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    /**
     * 查询商品信息
     * 
     * @param prodId 商品信息主键
     * @return 商品信息
     */
    @Override
    public TbProd selectTbProdByProdId(Integer prodId)
    {
        return tbProdMapper.selectTbProdByProdId(prodId);
    }

    /**
     * 查询商品信息列表
     * 
     * @param tbProd 商品信息
     * @return 商品信息
     */
    @Override
    public List<TbProd> selectTbProdList(TbProd tbProd)
    {
        return tbProdMapper.selectTbProdList(tbProd);
    }

    /**
     * 新增商品信息
     * 
     * @param tbProd 商品信息
     * @return 结果
     */
    @Override
    public int insertTbProd(TbProd tbProd)
    {
        tbProd.setCreateTime(DateUtils.getNowDate());
        return tbProdMapper.insertTbProd(tbProd);
    }

    /**
     * 修改商品信息
     * 
     * @param tbProd 商品信息
     * @return 结果
     */
    @Override
    public int updateTbProd(TbProd tbProd)
    {
        tbProd.setUpdateTime(DateUtils.getNowDate());
        return tbProdMapper.updateTbProd(tbProd);
    }

    /**
     * 批量删除商品信息
     * 
     * @param prodIds 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteTbProdByProdIds(Integer[] prodIds)
    {
        return tbProdMapper.deleteTbProdByProdIds(prodIds);
    }

    /**
     * 删除商品信息信息
     * 
     * @param prodId 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteTbProdByProdId(Integer prodId)
    {
        return tbProdMapper.deleteTbProdByProdId(prodId);
    }

    @Override
    public AjaxResult addAtta(ProdAtta prodAtta) {
        tbProdMapper.addAtta(prodAtta);
        return AjaxResult.success("商品附件添加成功");
    }

    @Override
    public AjaxResult getProdInfo(Integer infoId) {
        tbProdMapper.getProdInfo(infoId);
        return AjaxResult.success("商品详情查询成功");
    }

    @Override
    public List<TbProdInfo> getProdInfoList(Integer prodId) {

        return tbProdMapper.getProdInfoList(prodId);
    }

    @Override
    public AjaxResult addProdInfo(TbProdInfo tbProdInfo) {
        tbProdMapper.addProdInfo(tbProdInfo);
        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult createProdIndex() {
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(EsProd.class);
        if (indexOps.exists()){
            //有索引删除
            indexOps.delete();
        }
            //创建索引
            indexOps.create();
            //创建映射
            indexOps.putMapping(indexOps.createMapping());
            return AjaxResult.success("索引创建成功");
    }

    @Override
    public AjaxResult syncProdIndex() {
        //获取所有信息
        List<TbProd> tbProds = tbProdMapper.selectTbProdList(new TbProd());
        for (TbProd tbProd : tbProds) {
            EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
            elasticsearchRestTemplate.save(esProd);
        }
        return AjaxResult.success("同步成功");
    }

    @Override
    public TableDataInfo getListFromEs(TbProd tbProd, Integer pageNum, Integer pageSize) {
        //构建分页
        Integer from = 0;
        if (pageNum>0){
            from=(pageNum-1);
        }
        PageRequest pageRequest = PageRequest.of(from, pageSize);
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        //Es查询
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(pageRequest)
                .withQuery(boolQueryBuilder);
        SearchHits<EsProd> hits = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), EsProd.class);
        ArrayList<EsProd> list = new ArrayList<>();
        for (SearchHit<EsProd> hit : hits.getSearchHits()) {
            list.add(hit.getContent());
        }
        //返回分页数据
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setRows(list);
        dataInfo.setMsg("查询成功");
        dataInfo.setTotal(hits.getTotalHits());
        return dataInfo;
    }
}
